<template>
  <div class="closing">
    <div>
      <el-button type="primary" size="mini" @click="closing(1)">{{ $t('market.closeDown2') }}</el-button>
      <el-button type="primary" size="mini" @click="closing(2)">{{ $t('market.theClosing2') }}</el-button>
    </div>
    <div class="header">
      <div class="header-name">
        <search :options="selectData" :msg="tacitly" @seek="collect" @hand="handleQuerySonClass" />
        <el-button type="primary" class="btn" size="mini" @click="handleQuerySonClass">{{ $t('btn.search') }}</el-button>
        <filtration
          :ftype="ftype"
          :msg="tacitly"
          :select-data="selectData"
          :table-header="tableHeader1"
          :detail-header="tableHeader"
          @seachData="handleQuerySonClass"
        />
      </div>
      <!--      当前关账期间-->
      <dataRow ref="dataRow" />
    </div>
    <div class="table-content">
      <jc-table
        v-loading="loading"
        :table-data="tableData"
        :table-header="tableHeader"
        :table-height="tableHeight"
        serial
      >
        <!--  处理进度  -->
        <template v-slot:billSlot="col">
          <span v-if="col.scope.row.ftype === 2">{{ $t('market.theClosing') }}</span>
          <span v-if="col.scope.row.ftype === 1">{{ $t('market.closeDown') }}</span>
        </template>
        <!--  处理进度  -->
        <template v-slot:btnState="col">
          <el-tag v-if="col.scope.row.fisfinished === 0" type="warning">{{ $t('market.calculating') }}</el-tag>
          <el-tag v-if="col.scope.row.fisfinished === 1" type="success">{{ $t('market.accomplish') }}</el-tag>
        </template>
        <!--  处理结果  -->
        <template v-slot:btnStates="col">
          <el-tag v-if="col.scope.row.fissuccess === 0" type="danger">{{ $t('market.defeated') }}</el-tag>
          <el-tag v-if="col.scope.row.fissuccess === 1" type="success">{{ $t('market.succeed') }}</el-tag>
        </template>
        <!--  操作  -->
        <template v-slot:btnSlot="col">
          <el-button size="mini" type="primary" @click="closingDetail(col.scope.row.ftaskId)">{{ $t('market.detail') }}</el-button>
        </template>
      </jc-table>
    </div>
    <!--  分页器  -->
    <div class="footer">
      <jc-pagination
        :total="total"
        :page.sync="currentPage"
        :limit.sync="size"
        @pagination="handleGetUntreated"
      />
    </div>
    <!-- 关账详情 -->
    <el-dialog
      v-dialogDrag
      :title="$t('market.closeDownDetail')"
      model
      :visible.sync="isClosingDialog"
      :close-on-click-modal="false"
      width="1000px"
    >
      <div>
        <el-table :data="closingData" border max-height="700px">
          <el-table-column :label="$t('market.resultExecute')" align="center" width="100">
            <template slot-scope="scope">
              <el-tag v-if="scope.row.isSuccess === 0" type="danger">{{ $t('market.abnormal') }}</el-tag>
              <el-tag v-if="scope.row.isSuccess === 1" type="success">{{ $t('market.succeed') }}</el-tag>
            </template>
          </el-table-column>
          <el-table-column :label="$t('market.executeDetail')" prop="fdescripTion" align="center" />
        </el-table>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  queryClosetaskList, queryNextClose,
  queryNextAnticlose,
  queryCloseTaskDetails, createPayRecClosetask, createAnPayRecClosetask
} from '@/api/financial/closing'
import search from '@/components/Search'
import searData from '@/components/Search/mixin'
import filtration from '@/components/filtration'
import dataRow from '@/views/financial/company/components/dataRow'
export default {
  name: 'Closing',
  components: {
    search,
    filtration,
    dataRow
  },
  mixins: [searData],
  data() {
    return {
      ftype: 34,
      total: 0, // 总条目
      currentPage: 1, // 当前页
      size: 200, // 每页显示多少条数据
      isClosingDialog: false,
      closingData: [],
      // 表头
      tableData: [], // 销售数据
      tableHeight: '70vh',
      tableHeader: [
        { label: this.$t('market.period'), prop: 'fdate' },
        { label: this.$t('shop.type'), type: 'bill' },
        { label: this.$t('market.companyname'), prop: 'fcompanyname' },
        { label: this.$t('market.closeor'), prop: 'fcloserName' },
        { label: this.$t('market.closeyDate'), prop: 'fclosetime' },
        { label: this.$t('market.progress'), type: 'state', prop: 'fisfinished' },
        { label: this.$t('market.finishTime'), prop: 'ffinishedtime' },
        { label: this.$t('market.issuccess'), type: 'states', prop: 'fissuccess' },
        { label: this.$t('market.handle'), type: 'btn', width: '100' }
      ],
      tableHeader1: []
    }
  },
  created() {
    this.handleGetUntreated()
    this.tableHeader1 = this.tableHeader
    this.tableHeight = window.innerWidth > 1440 ? 'calc(100vh - 260px)' : '73vh'
  },
  methods: {
    // 获取列表数据
    async handleGetUntreated(ev) {
      this.loading = true
      this.details = []
      if (ev && !ev.page) {
        this.details = ev
      }
      const DATA = {
        pageNum: this.currentPage,
        pageSize: this.size,
        fclosetype: 1,
        search: this.searCollData.concat(this.details)
      }
      const RES = await queryClosetaskList(DATA)
      if (RES.code === 0) {
        this.loading = false
        this.tableData = RES.data.list
        this.total = RES.data.total
      } else {
        this.loading = false
        this.$confirm(RES.message, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'error'
        })
      }
    },
    // 搜索
    handleQuerySonClass(ev) {
      this.currentPage = 1
      if (ev && ev.fname) {
        this.customHandle(ev)
        this.handleGetUntreated(ev.detail)
      } else {
        this.handleGetUntreated()
        this.$refs.dataRow.queryCompany()
      }
    },
    // 关账
    closing(ev) {
      const api = ev === 1 ? queryNextClose : queryNextAnticlose
      // 查询关账日期
      api({ fclosetype: 1 }).then((res) => {
        if (res.code === 0) {
          const message = ev === 1 ? '应收款关账期间:' : '应收款反关账期间:'
          this.$confirm(message + res.data, '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(() => {
            const api1 = ev === 1 ? createPayRecClosetask : createAnPayRecClosetask
            // 创建关账任务
            api1({ fclosetype: 1 }).then((res) => {
              if (res.code === 0) {
                this.handleGetUntreated()
                this.$message({ showClose: true, message: res.message, type: 'success' })
              } else {
                this.$confirm(res.message, '提示', {
                  confirmButtonText: '确定',
                  cancelButtonText: '取消',
                  type: 'error'
                })
              }
            })
          })
        }
      })
    },
    // 详情
    closingDetail(id) {
      this.isClosingDialog = true
      queryCloseTaskDetails({ ftaskId: id }).then((res) => {
        if (res.code === 0) {
          this.closingData = res.data.list
        } else {
          this.$confirm(res.message, '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'error'
          })
        }
      })
    },
    // 自定义列表
    customHandle(ev) {
      this.tableHeaders = []
      if (ev.header) {
        ev.header.forEach(item => {
          this.tableHeader1.forEach(res => {
            if (res.label === item) {
              this.tableHeaders.push(res)
            }
          })
        })
        this.tableHeader = this.tableHeaders
      } else if (ev.fname === '缺省方案') {
        this.tableHeader = this.oldTabHeader
      } else {
        this.tableHeader = this.tableHeader1
      }
    }
  }
}
</script>
<style lang="scss" scoped>
.closing {
  @include listBom;

  .header{
    position:relative;
    .header-name{
      width: 100%;
    }
    .btn{
      transform: translateY(18%);
      margin-left: 450px !important;
    }
  }
}
</style>
